<?php

namespace app\model;

use think\Model;
use app\model\Task;
use app\model\Staff;
use app\model\Enter;

class OnJob extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * 输送人才
     *
     * @param [type] $task_id
     * @param [type] $staff_id
     * @return void
     */
    public static function sendStaff($user_id, $task_id, $staff_id)
    {
        $task = Task::where('id', $task_id)->where('is_show', 1)->whereOr('delete_time', '<>', null)->find();
        if (!empty($task)) {
            error('此任务已经下架');
        }

        // 启动事务
        self::startTrans();
        try {
            foreach ($staff_id as $k => $v) {
                $staff_info = Staff::field('id, name, id_num, phone')->where('id', $v)->find();
                if (self::where('staff_id', $v)->where('type', 1)->find()) {
                    error($staff_info['name'] . '已被录用请重新挑起人才');
                }
                $is_send = self::where('staff_id', $v)->where('task_id', $task_id)->find();
                if (empty($is_send)) {
                    $user_info = Enter::where('user_id', $user_id)->find();
                    if ($user_info['shenfen'] == 1) {
                        $post_houser = $user_info['enter_sheng'] . ',' . $user_info['enter_shi'];
                    } elseif ($user_info['shenfen'] == 2) {
                        $post_houser = $user_info['enter_sheng'] . ',' . $user_info['enter_shi'] . ',' . $user_info['enter_xian'];
                    } elseif ($user_info['shenfen'] == 3) {
                        $post_houser = $user_info['enter_sheng'] . ',' . $user_info['enter_shi'] . ',' . $user_info['enter_xian'] . ',' . $user_info['enter_xiang'];
                    }

                    self::create(['staff_id' => $v, 'phone' => $staff_info['phone'], 'name' => $staff_info['name'], 'id_num' => $staff_info['id_num'], 'task_id' => $task_id, 'type' => 0, 'post_houser' => $post_houser]);
                } else {
                    if ($is_send['type'] == 2) {
                        self::where('id', $is_send['id'])->update(['type' => 0]);
                    }
                }
            }
            // 提交事务
            self::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            self::rollback();
            error($e->getMessage());
        }
    }

    /**
     * 导入离职人员
     *
     * @param [type] $data
     * @return void
     */
    public static function createLeaveJobByExcel($data)
    {
        // 启动事务
        self::startTrans();
        try {
            
            $i = 0;
            foreach ($data as $k => $v) {
                $i++;
                $is_exist = self::where('id_num', $v['B'])->where('type', 1)->find();
                if (!empty($is_exist)) {
                    self::where('id', $is_exist['id'])
                        ->update(['leave_time' => $v['D'], 'cause' => $v['E'], 'type' => 3]);
                } else {
                    self::rollback();
                    error('第'.$i.'条数据错误, 此人未入职');
                }
            }
            // 提交事务
            self::commit();
            return $i;
        } catch (\Exception $e) {
            // 回滚事务
            self::rollback();
            // error($e->getMessage());
            error('第'.$i.'条数据错误');
        }
    }
}
